package com.example.consumer.controller;

import com.example.resp.CommonResp;
import com.example.pojo.User;
import com.example.req.LoginUserReq;
import com.example.req.LoginUserSaveReq;
import com.example.resp.LoginUserResp;
import com.example.feignapi.service.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;


@RestController
@RequestMapping("/login-user")
public class TestController {
    @Autowired
    private UserClient userClient;

    @PostMapping("register")
    public CommonResp register(@Valid @RequestBody LoginUserSaveReq req){
        req.setUserpassword(DigestUtils.md5DigestAsHex(req.getUserpassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        userClient.register(req);
        return resp;
    }

    @PostMapping("login")
    public CommonResp login(@Valid @RequestBody LoginUserReq req){
        req.setUserpassword(DigestUtils.md5DigestAsHex(req.getUserpassword().getBytes()));
        CommonResp<LoginUserResp> resp = new CommonResp<>();
        LoginUserResp loginUserResp = userClient.login(req);
        resp.setContent(loginUserResp);
        return resp;
    }
}
